package com.polaris.live.im.sfs.processor.impl

import com.polaris.live.im.manager.ImLiveManager
import com.polaris.live.im.sfs.COMMAND_LIVE_CLOSE
import com.polaris.live.im.sfs.COMMAND_LIVE_FLOATING
import com.polaris.live.im.sfs.COMMAND_LIVE_MESSAGE
import com.polaris.live.im.sfs.COMMAND_LIVE_NOTICE
import com.polaris.live.im.sfs.LIVE_NOTICE_MESSAGE
import com.polaris.live.im.sfs.LIVE_TOAST_MESSAGE
import com.polaris.live.im.sfs.TYPE_LIVE_MESSAGE
import com.polaris.live.im.sfs.core.bean.DataArgument
import com.polaris.live.im.sfs.processor.AbstractMessageProcessor
import com.polaris.live.im.utils.sfsObjectLiveMessageRequest
import sfs2x.client.core.SFSEvent

/**
 * 直播事件
 *
 * @author Created by lucas on 2023/10/19 16:56
 * @since 1.0.0
 */
class LiveEventMessageProcessor : AbstractMessageProcessor() {

    override fun shouldProcess(type: Int, command: String): Boolean {
        return command in commands
    }

    override suspend fun handle(type: Int, command: String, argument: DataArgument) {
        when (command) {
            COMMAND_LIVE_MESSAGE -> {
                val liveMessage = argument.getData {
                    sfsObjectLiveMessageRequest(it)
                }
                if (liveMessage?.businessType == TYPE_LIVE_MESSAGE) {
                    ImLiveManager.onPublicMessage(liveMessage)

                }
            }

            SFSEvent.ROOM_VARIABLES_UPDATE -> {
                ImLiveManager.onVariableUpdate(argument)
            }

            SFSEvent.ROOM_JOIN, SFSEvent.USER_ENTER_ROOM, SFSEvent.USER_EXIT_ROOM -> {
                ImLiveManager.onEvent(command, argument)
            }

            else -> {
                ImLiveManager.onMessage(command, argument)
            }
        }
    }

    companion object {

        val commands = setOf(
            COMMAND_LIVE_MESSAGE,
            SFSEvent.ROOM_VARIABLES_UPDATE,
            SFSEvent.USER_ENTER_ROOM,
            SFSEvent.USER_EXIT_ROOM,
            SFSEvent.ROOM_JOIN,

            //这里是其他的消息
            COMMAND_LIVE_CLOSE,
            COMMAND_LIVE_NOTICE,
            COMMAND_LIVE_FLOATING,
        )

        val publicMessages = setOf(
            LIVE_TOAST_MESSAGE,
            LIVE_NOTICE_MESSAGE,
        )
    }
}